@import WebKit;
#import "dfhgwhruebvhrheynuisanceimply.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyimplysuppose.h"
#import "dfhgwhruebvhrheybangorganismViewController.h"
#import "dfhgwhruebvhrheyblastwhistleViewController.h"
@interface dfhgwhruebvhrheybangorganismViewController ()<WKNavigationDelegate, WKScriptMessageHandler>
@property (nonatomic, copy) NSString *serviceLink;
@property (nonatomic, strong) WKWebView *wkWebView;
@end
@implementation dfhgwhruebvhrheybangorganismViewController {
	UIView *bgView;
	UIView *bottomView;
	UIButton *confirmBtn;
	void(^_action)(UIButton *sender);
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"AppModel"];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"AppModel"];
}
- (void)setBottomTitle:(NSString *)bottomTitle {
	_bottomTitle = bottomTitle;
	[confirmBtn setTitle:bottomTitle forState:UIControlStateNormal];
}
- (WKWebView *)wkWebView {
	if(!_wkWebView) {
		WKPreferences *preference = [[WKPreferences alloc] init];
		preference.minimumFontSize = 15;
		preference.javaScriptCanOpenWindowsAutomatically = YES;
		WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
		wkWebConfig.preferences = preference;
		wkWebConfig.userContentController = [[WKUserContentController alloc] init];
		_wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebConfig];
	}
	return _wkWebView;
}
- (void)viewDidLoad {
	[super viewDidLoad];
	[self loadUI];
}
- (void)loadUI {
	self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
    if (self.isRoot) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-26, 44)];
        titleLabel.text = self.navigationItem.title;
        titleLabel.font = [UIFont semiBold:20];
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
        [self loadURLString:serviceH5];
    }else {
        [self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
    }
	bgView = [[UIView alloc] init];
	bgView.backgroundColor = [UIColor whiteColor];
	[self.view addSubview:bgView];
	self.wkWebView.navigationDelegate = self;
	[self.view addSubview:self.wkWebView];
	if(self.isShowBottomView) {
		bottomView = [[UIView alloc] init];
		[self.view addSubview:bottomView];
		confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
		confirmBtn.layer.cornerRadius = 10;
		confirmBtn.titleLabel.font = [UIFont semiBold:18];
		confirmBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
		NSString *title = self.bottomTitle.length ? self.bottomTitle : @"I have read and agree";
		[confirmBtn setTitle:title forState:UIControlStateNormal];
		[confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		[confirmBtn addTarget:self action:@selector(confirmBtnAction:) forControlEvents:UIControlEventTouchUpInside];
		[bottomView addSubview:confirmBtn];
	}
}
- (void)serviceLinkRequest {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:serviceLinkApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheyimplysuppose *model = [dfhgwhruebvhrheyimplysuppose yy_modelWithJSON:json];
		if (model.code == 200) {
			strongSelf.serviceLink = model.facebook_url;
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		NSLog(@"==%@", error);
	}];
}
- (void)loadURLString:(NSString *)urlString {
	if(!self.wkWebView) return;
	NSString *string;
	if ([urlString rangeOfString:[dfhgwhruebvhrheyluggagerecorder shareInstance].dhgfywegwygeydwurgentdizzy].location == NSNotFound) {
		string = [[dfhgwhruebvhrheyluggagerecorder shareInstance].dhgfywegwygeydwurgentdizzy stringByAppendingString:urlString];
	}else {
		string = urlString;
	}
	NSURL *url = [NSURL URLWithString:string];
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
	[self.wkWebView loadRequest:request];
	if ([urlString isEqualToString:serviceH5]) {
		[self serviceLinkRequest];
	}
}
- (void)bottomBtnsClick:(void(^)(UIButton *sender))callback {
	_action = callback;
}
- (void)confirmBtnAction:(UIButton *)sender {
	[self.navigationController popViewControllerAnimated:YES];
	if(_action) {
		_action(sender);
	}
}
- (void)backAction {
	if([self.wkWebView canGoBack]) {
		[self.wkWebView goBack];
	}else {
		[self.navigationController popViewControllerAnimated:YES];
	}
}
- (void)servicePage {
	dfhgwhruebvhrheyblastwhistleViewController *service = (dfhgwhruebvhrheyblastwhistleViewController *)[UIStoryboard storyboard:@"Mine" withidentifier:@"service"];
    service.hidesBottomBarWhenPushed = YES;
	[self.navigationController pushViewController:service animated:YES];
}
- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
		make.left.bottom.right.equalTo(self.view);
	}];
	[self.wkWebView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.top.right.equalTo(bgView);
		if(self.isShowBottomView) {
			make.bottom.equalTo(bottomView.mas_top);
		}else {
			make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
		}
	}];
	if(self.isShowBottomView) {
		[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
			make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
			make.left.right.equalTo(self.view);
			make.height.mas_equalTo(50);
		}];
		[confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
			make.edges.equalTo(bottomView).insets(UIEdgeInsetsMake(5, 25, 0, 25));
		}];
	}
}
#pragma mark: ====== WKNavigationDelegate ======
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
	[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
	[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSString *urlString = navigationAction.request.URL.absoluteString;
    if (self.isRoot) {
        if ([urlString containsString:serviceH5]) {
            decisionHandler(WKNavigationActionPolicyAllow);
        }else {
            dfhgwhruebvhrheybangorganismViewController *webVC = [[dfhgwhruebvhrheybangorganismViewController alloc] init];
            webVC.isShowBottomView = NO;
            [webVC loadURLString:urlString];
            webVC.hidesBottomBarWhenPushed = YES;
            webVC.navigationItem.title = @"Customer Service";
            [self.navigationController pushViewController:webVC animated:YES];
            decisionHandler(WKNavigationActionPolicyCancel);
        }
    }else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}
#pragma mark: ==== WKScriptMessageHandler ===
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
	if ([message.name isEqualToString:@"AppModel"]) {
		dfhgwhruebvhrheynuisanceimply *model = [dfhgwhruebvhrheynuisanceimply yy_modelWithJSON:message.body];
		if (model.type == 0) {
			if (self.serviceLink.length == 0) {
				return [self.view show:@"error service"];
			}
			NSURL *url = [NSURL URLWithString:self.serviceLink];
			if ([[UIApplication sharedApplication] canOpenURL:url]) {
				[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
			}
		}
		if (model.type == 2) {
			[self servicePage];
		}
	}
}
@end
